// @ts-ignore
import { NavigationGuardNext, RouteLocationNormalized } from "vue-router";
import { createRouterInterceptor, needRouterLoading, security, happyFramework } from "@va/framework";
import { setupToast } from "@va/primevue"
// @ts-ignore
const viewsModules = import.meta.glob('/src/views/**/**.vue')
function includes (matched: any) {
    if (whiteList.length>0){
        for (const matchedElement of matched) {
            if (whiteList.includes(matchedElement.path)) {
                return true
            }
        }
        return false
    }else{
        return true;
    }
}


// 创建默认的拦截器
const beforeInterceptor = createRouterInterceptor({
    async dataLoader () {
        const result = { rawData: null, message: '' }
        try {
            // const data = await request()
            // 实际开发环境应该从服务端拉取数据
            const data: any = await new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve({
                        code   : 0,
                        data   : [
                            {
                                name       : '控制台',
                                path       : '/',
                                view       : '/home/index.vue',
                                isRouter   : true,
                                isKeepalive: true,
                                icon       : 'ri-apps-line',
                                poster     : 'components.png',
                                type       : 'menu',
                                children   : []
                            },
                        ],
                        message: 'success'
                    })
                }, 1000)
            })
            result.rawData = data.data
            result.message = data.message
        } catch (e:any) {
            result.rawData = null
            result.message = e.message
        }
        return result
    },
    dataLoadFailureHandler(result, to, from, next) {
        // 失败处理
    },
    routerInjectOption: {
        randomName : true,
        parentRoute: {
            name     : 'BaseLayout',
            path     : '/',
            component: () => import('/src/layout/base-layout.vue')
        },
        routes: [],
        viewLoader(view:string) {
            const com = viewsModules[`/src/views${view}`]
            if (!com) {
                // return () => import('/src/views/error404/index.vue')
            }
            // 会出现警告
            // return defineAsyncComponent({
            //     loadingComponent: LoadPlaceholder,
            //     loader          : com
            // })
        }
    }
})
const whiteList: string[] = [];
export const onBeforeEach =  (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext)=>{
    if (!security.getToken()) {
        if (!includes(to.matched)) {
            needRouterLoading.value = false
            next('/login')
            return
        }
        needRouterLoading.value = false
        next()
        return
    }
    if (!includes( to.matched)) {
        needRouterLoading.value = !happyFramework.routerInitiated
        beforeInterceptor.filter(to, from, next)
    } else {
        next()
    }
}